package com.mall.shopping.services;

import com.mall.shopping.IHomeService;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.PanelContentDto;
import com.mall.shopping.dto.PanelContentItemDto;
import com.mall.shopping.dto.PanelDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ergwang
 * @date 2020-7-10 16:57
 * @com.mall.shopping.Impl
 */
@Service
@Component
public class IHomeServiceImpl implements IHomeService {

    @Autowired
    private PanelContentMapper panelContentMapper;

    @Autowired
    private PanelMapper panelMapper;

    @Autowired
    private ContentConverter converter;

    @Override
    public List<PanelDto> homepage() {
        // 先查询panel的数据
        List<Panel> panels = panelMapper.selectAll();
        // 利用for循环根据结果集的panelId来查询对应的Items参数,并把panel转换为panelDto
        List<PanelDto> panelDtos = new ArrayList<PanelDto>();
        for (Panel panel : panels) {
            // 将panel转换为panelDto
            PanelDto panelDto = converter.panen2Dto(panel);
            // 根据id获取items 并转换为Dto
            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
            List<PanelContentItemDto> itemDtoList = converter.panelContentItem2Dto(panelContentItems);
            // 将item存入panelDto中，并将Dto存入数组中
            panelDto.setPanelContentItems(itemDtoList);
            panelDtos.add(panelDto);
        }

        return panelDtos;
    }

    /**
     * 导航栏
     * @return
     */
    @Override
    public List<PanelContentDto> navigation() {
        List<PanelContent> panelContents = panelContentMapper.selectPanelContentByPanelId(0);
        // 转换
        List<PanelContentDto> panelContentDtos = converter.panelContents2Dto(panelContents);
        return panelContentDtos;
    }
}
